package DTO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import util.DBUtil;

/**
 * <p>Java class for Task complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="Task">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="taskID" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *         &lt;element name="taskName" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="minestoneID" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *         &lt;element name="username" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="description" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="startDay" type="{http://xml.netbeans.org/schema/Tasks}Date"/>
 *         &lt;element name="endDay" type="{http://xml.netbeans.org/schema/Tasks}Date"/>
 *         &lt;element name="status" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Task", propOrder = {
    "taskID",
    "taskName",
    "minestoneID",
    "username",
    "description",
    "startDay",
    "endDay",
    "status"
})
public class Task {

    protected int taskID;
    @XmlElement(required = true)
    protected String taskName;
    protected int minestoneID;
    @XmlElement(required = true)
    protected String username;
    @XmlElement(required = true)
    protected String description;
    @XmlElement(required = true)
    protected Date startDay;
    @XmlElement(required = true)
    protected Date endDay;
    @XmlElement(required = true)
    protected String status;

    public Task(int taskID, String taskName, int minestoneID, String username, String description, Date startDay, Date endDay, String status) {
        this.taskID = taskID;
        this.taskName = taskName;
        this.minestoneID = minestoneID;
        this.username = username;
        this.description = description;
        this.startDay = startDay;
        this.endDay = endDay;
        this.status = status;
    }

    public Task() {
    }

    public Task(String taskName, int minestoneID, String username, String description, Date startDay, Date endDay, String status) {
        this.taskName = taskName;
        this.minestoneID = minestoneID;
        this.username = username;
        this.description = description;
        this.startDay = startDay;
        this.endDay = endDay;
        this.status = status;
    }

    /**
     * Gets the value of the taskID property.
     * 
     */
    public int getTaskID() {
        return taskID;
    }

    /**
     * Sets the value of the taskID property.
     * 
     */
    public void setTaskID(int value) {
        this.taskID = value;
    }

    /**
     * Gets the value of the taskName property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getTaskName() {
        return taskName;
    }

    /**
     * Sets the value of the taskName property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setTaskName(String value) {
        this.taskName = value;
    }

    /**
     * Gets the value of the minestoneID property.
     * 
     */
    public int getMinestoneID() {
        return minestoneID;
    }

    /**
     * Sets the value of the minestoneID property.
     * 
     */
    public void setMinestoneID(int value) {
        this.minestoneID = value;
    }

    /**
     * Gets the value of the username property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUsername() {
        return username;
    }

    /**
     * Sets the value of the username property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUsername(String value) {
        this.username = value;
    }

    /**
     * Gets the value of the description property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getDescription() {
        return description;
    }

    /**
     * Sets the value of the description property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setDescription(String value) {
        this.description = value;
    }

    /**
     * Gets the value of the startDay property.
     * 
     * @return
     *     possible object is
     *     {@link Date }
     *     
     */
    public Date getStartDay() {
        return startDay;
    }

    /**
     * Sets the value of the startDay property.
     * 
     * @param value
     *     allowed object is
     *     {@link Date }
     *     
     */
    public void setStartDay(Date value) {
        this.startDay = value;
    }

    /**
     * Gets the value of the endDay property.
     * 
     * @return
     *     possible object is
     *     {@link Date }
     *     
     */
    public Date getEndDay() {
        return endDay;
    }

    /**
     * Sets the value of the endDay property.
     * 
     * @param value
     *     allowed object is
     *     {@link Date }
     *     
     */
    public void setEndDay(Date value) {
        this.endDay = value;
    }

    /**
     * Gets the value of the status property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getStatus() {
        return status;
    }

    /**
     * Sets the value of the status property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setStatus(String value) {
        this.status = value;
    }

    public boolean UpdateTask() {
        DBUtil util = new DBUtil();
        Connection conn = null;
        PreparedStatement stm = null;
        try {
            Class.forName(util.getDriverName());
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            conn = DriverManager.getConnection(util.getURL(), util.getUsername(), util.getPassword());
            if (conn != null) {

                String sql = "UPDATE Tasks SET [TaskName] = ?,[Description] = ?,[StartDay] = ?,[EndDay] = ?,[Status] = ? WHERE TaskID = ?";
                stm = conn.prepareStatement(sql);
                stm.setString(1, this.getTaskName());
                stm.setString(2, this.getDescription());
                stm.setDate(3, this.getStartDay().getDateJavaUtil());
                stm.setDate(4, this.getEndDay().getDateJavaUtil());
                stm.setString(5, this.getStatus());
                stm.setInt(6, this.getTaskID());

                int row = stm.executeUpdate();
                if (row > 0) {
                    return true;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(Task.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Project.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public Task InsertTask() throws ClassNotFoundException {
        DBUtil util = new DBUtil();
        Connection conn = null;
        try {
            Class.forName(util.getDriverName());
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            conn = DriverManager.getConnection(util.getURL(), util.getUsername(), util.getPassword());
            PreparedStatement stm = null;
            if (conn != null) {
                int returnID = -1;

                String sql = "INSERT INTO [Tasks]([TaskName],[MinestoneID],[username],[Description],[StartDay],[EndDay],[Status]) VALUES (?,?,?,?,?,?,?)";
                stm = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                stm.setString(1, this.taskName);
                stm.setInt(2, this.minestoneID);
                stm.setString(3, this.username);
                stm.setString(4, this.description);
                stm.setDate(5, this.startDay.getDateJavaUtil());
                stm.setDate(6, this.endDay.getDateJavaUtil());
                stm.setString(7, this.status);
                stm.executeUpdate();

                ResultSet rs = stm.getGeneratedKeys();
                if (rs.next()) {
                    returnID = rs.getInt(1);
                }

                if (returnID != -1) {
                    this.setTaskID(returnID);
                }
                rs.close();
                stm.close();
                conn.close();
                return this;
            }
            return null;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
